﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AspireApp9.AppHost
{
    public class ResourceContainer(string name) : ContainerResource(name), IResourceWithConnectionString
    {
        internal const string SmtpEndpointName = "smtp";
        internal const string HttpEndpointName = "http";

        private EndpointReference? _httpReference;

        public EndpointReference HttpEndpoint =>
            _httpReference ??= new(this, HttpEndpointName);
        public ReferenceExpression ConnectionStringExpression =>
            ReferenceExpression.Create(
                $"http://{HttpEndpoint.Property(EndpointProperty.Host)}:{HttpEndpoint.Property(EndpointProperty.Port)}"
            );
    }

    public static class MailDevResourceBuilderExtensions
    {

        public static IResourceBuilder<ResourceContainer> AddResourceContainerDev(
            this IDistributedApplicationBuilder builder,
            string name,
            int? httpPort = null,
            int? smtpPort = null)
        {

            var resource = new ResourceContainer(name);

            return builder.AddResource(resource) // 添加容器资源
                                                 // 容器镜像
                          .WithImage(MailDevContainerImageTags.Image)
                          .WithImageRegistry(MailDevContainerImageTags.Registry)
                          .WithImageTag(MailDevContainerImageTags.Tag)
                          // 暴露的节点 
                          .WithHttpEndpoint(
                              targetPort: 8080,
                              port: httpPort,
                              name: ResourceContainer.HttpEndpointName);
                          //.WithEndpoint(
                          //    targetPort: 1025,
                          //    port: smtpPort,
                          //    name: resource.SmtpEndpointName);
        }
    }


    internal static class MailDevContainerImageTags
    {
        internal const string Registry = "/var/lib/docker/images";

        internal const string Image = "webapi";

        internal const string Tag = "dev";
    }
}
